package GUILayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.TitledBorder;

import ControlLayer.RoomTypeCtr;
import GUIHelper.RoomTypeDeletePanel;
import GUIHelper.RoomTypeInfoPanel;
import ModelLayer.RoomType;

//for creating room type panel
public class RoomTypeGUI extends JPanel {

	private static final long serialVersionUID = 1L;
	private RoomTypeCtr rtCtr;
	private LinkedList<RoomType> rts;

	public RoomTypeGUI() {
		rtCtr = new RoomTypeCtr();
		rts = new LinkedList<RoomType>();
		initialize();
	}

	public void initialize() {
		removeAll(); // for delete a room type later on to repaint the whole
						// panel with less room type tabs
		rts = rtCtr.getAllRoomType();

		this.setLayout(new BorderLayout());
		JPanel topMenu = new JPanel();
		this.add(topMenu, BorderLayout.NORTH);
		topMenu.setLayout(new FlowLayout(FlowLayout.LEFT));

		JButton btnCreate = new JButton("CreateRoomType");

		topMenu.add(btnCreate);

		JTabbedPane subMenu = new JTabbedPane();
		TitledBorder title = BorderFactory.createTitledBorder("RoomType");
		title.setTitleJustification(TitledBorder.CENTER);
		subMenu.setBorder(title);

		subMenu.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
		this.add(subMenu, BorderLayout.CENTER);

		for (RoomType rt : rts) {
			JPanel rtPanel = new RoomTypeModelGUI(rt, this);
			String name = rt.getName().toString() + rt.getSize();
			subMenu.add(name, rtPanel);
		}

		btnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createActionPerformed(e);
			}
		});

	}

	public void createActionPerformed(ActionEvent e) {
		JFrame createFrame = createFrame(this);
		createFrame.setVisible(true);
	}

	public void deleteActionPerformed(ActionEvent e) {
		JFrame deleteFrame = deleteFrame(this);
		deleteFrame.setVisible(true);
	}

	public JFrame deleteFrame(JPanel panel) {
		final JFrame createPupupWindow = new JFrame("Delete RoomType");
		createPupupWindow.setSize(300, 100);
		createPupupWindow.setLocationRelativeTo(null);
		RoomTypeDeletePanel deletePanel = new RoomTypeDeletePanel(panel);
		createPupupWindow.add(deletePanel);
		return createPupupWindow;
	}

	public JFrame createFrame(JPanel rtGUI) {
		final JFrame createPupupWindow = new JFrame("Create RoomType");
		createPupupWindow.setSize(300, 200);
		createPupupWindow.setLocationRelativeTo(null);
		RoomTypeInfoPanel roomTypeInfo = new RoomTypeInfoPanel(rtGUI);
		createPupupWindow.add(roomTypeInfo);
		return createPupupWindow;
	}
}
